package cn.peach.org.service.impl;

import cn.peach.basic.exception.MyException;
import cn.peach.basic.service.impl.BaseServiceImpl;
import cn.peach.org.domain.Employee;
import cn.peach.org.domain.Shop;
import cn.peach.org.mapper.EmployeeMapper;
import cn.peach.org.mapper.ShopMapper;
import cn.peach.org.service.IEmployeeService;
import cn.peach.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void settledIn(Shop shop) {
        // 对用户进行校验 判断用户是否存在
        Employee admin = shop.getAdmin();
        // 验证两次密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new MyException("密码不一致");
        }
        // 判断用户名是否存在
        Employee employee = employeeMapper.findByUser(admin.getUsername());
        if (employee != null)
            throw new MyException("用户已存在");
            // 有两张表 先添加employee 保存管理员信息 并返回自增id
            employeeMapper.add(admin);
            // 给shop里面添加employee的自增id
            shop.setAdmin(admin);
            shopMapper.add(shop);
            // 更新employee表中的shop_id
            admin.setShop_id(shop.getId());
            employeeMapper.update(admin);

    }
}
